package org.helldorado.companybuilder;

import java.util.List;
import org.helldorado.companybuilder.R;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TextView.BufferType;

public class WeaponAdapter{

	private Context mContext;
	private View mCurrentView;
	
	public WeaponAdapter(Context ctx, View currentView) {
		mContext = ctx;
		mCurrentView = currentView;
	}
	
	private void ShowHitRows(View weaponView, TIPDAdapter tipdAdapter, Weapon weapon)
	{
		TableRow tblRowHitsDamage = (TableRow)weaponView.findViewById(R.id.idHitsDamageRow);
		TableRow tblRowHitDamage1 = (TableRow)weaponView.findViewById(R.id.idHitDamage1Row);
		TableRow tblRowHitDamage2 = (TableRow)weaponView.findViewById(R.id.idHitDamage2Row);
		TableRow tblRowHitDamage3 = (TableRow)weaponView.findViewById(R.id.idHitDamage3Row);
		TableRow tblRowHitDamage4 = (TableRow)weaponView.findViewById(R.id.idHitDamage4Row);
		TableRow tblRowHitDamage5 = (TableRow)weaponView.findViewById(R.id.idHitDamage5Row);
		
		tblRowHitsDamage.setVisibility(View.VISIBLE);
		tblRowHitDamage1.setVisibility(View.VISIBLE);
		tblRowHitDamage2.setVisibility(View.VISIBLE);
		tblRowHitDamage3.setVisibility(View.VISIBLE);
		tblRowHitDamage4.setVisibility(View.VISIBLE);
		tblRowHitDamage5.setVisibility(View.VISIBLE);
		
		tblRowHitDamage1.removeAllViews();
		tblRowHitDamage2.removeAllViews();
		tblRowHitDamage3.removeAllViews();
		tblRowHitDamage4.removeAllViews();
		tblRowHitDamage5.removeAllViews();
		
		LayoutInflater inflater = LayoutInflater.from(mContext);
		TextView idHit1 = (TextView) inflater.inflate(R.layout.centered_textview, (ViewGroup)tblRowHitDamage1, false);
		idHit1.setText("1");
		tblRowHitDamage1.addView(idHit1);
		tipdAdapter.AddTIPDsToLayout(weapon.getHitTIPDs(1), "", tblRowHitDamage1, " + ");
		
		TextView idHit2 = (TextView) inflater.inflate(R.layout.centered_textview, (ViewGroup)tblRowHitDamage2, false);
		idHit2.setText("2");
		tblRowHitDamage2.addView(idHit2);
		tipdAdapter.AddTIPDsToLayout(weapon.getHitTIPDs(2), "", tblRowHitDamage2, " + ");
		
		TextView idHit3 = (TextView) inflater.inflate(R.layout.centered_textview, (ViewGroup)tblRowHitDamage3, false);
		idHit3.setText("3");
		tblRowHitDamage3.addView(idHit3);
		tipdAdapter.AddTIPDsToLayout(weapon.getHitTIPDs(3), "", tblRowHitDamage3, " + ");
		
		TextView idHit4 = (TextView) inflater.inflate(R.layout.centered_textview, (ViewGroup)tblRowHitDamage4, false);
		idHit4.setText("4");
		tblRowHitDamage4.addView(idHit4);
		tipdAdapter.AddTIPDsToLayout(weapon.getHitTIPDs(4), "", tblRowHitDamage4, " + ");
		
		TextView idHit5 = (TextView) inflater.inflate(R.layout.centered_textview, (ViewGroup)tblRowHitDamage5, false);
		idHit5.setText("5");
		tblRowHitDamage5.addView(idHit5);
		tipdAdapter.AddTIPDsToLayout(weapon.getHitTIPDs(5), "", tblRowHitDamage5, " + ");
	}
	
	private void HideHitRows(View weaponView)
	{
		TableRow tblRowHitsDamage = (TableRow)weaponView.findViewById(R.id.idHitsDamageRow);
		TableRow tblRowHitDamage1 = (TableRow)weaponView.findViewById(R.id.idHitDamage1Row);
		TableRow tblRowHitDamage2 = (TableRow)weaponView.findViewById(R.id.idHitDamage2Row);
		TableRow tblRowHitDamage3 = (TableRow)weaponView.findViewById(R.id.idHitDamage3Row);
		TableRow tblRowHitDamage4 = (TableRow)weaponView.findViewById(R.id.idHitDamage4Row);
		TableRow tblRowHitDamage5 = (TableRow)weaponView.findViewById(R.id.idHitDamage5Row);
		
		tblRowHitsDamage.setVisibility(View.GONE);
		tblRowHitDamage1.setVisibility(View.GONE);
		tblRowHitDamage2.setVisibility(View.GONE);
		tblRowHitDamage3.setVisibility(View.GONE);
		tblRowHitDamage4.setVisibility(View.GONE);
		tblRowHitDamage5.setVisibility(View.GONE);
	}

	public void SetWeapon(Weapon weapon, View weaponView, TIPDAdapter tipdAdapter){
		//View weapon1View = mCurrentView.findViewById(R.id.weapon1);
		//TextView txtName = (TextView)weapon1View.findViewById(R.id.idWeaponName);
		TextView txtName = (TextView)weaponView.findViewById(R.id.idWeaponName);
		txtName.setText(weapon.getTitle());
		TableRow tblRowAbilities = (TableRow)weaponView.findViewById(R.id.idWeaponAbilitiesRow);
		tblRowAbilities.removeAllViews();
		TableRow.LayoutParams params = new TableRow.LayoutParams();
		params.span = 2;
		tipdAdapter.AddTIPDsToLayout(weapon.getWeaponAbilities(), "", tblRowAbilities, ", ", params);
		if (weapon.hasHits())
		{
			ShowHitRows(weaponView, tipdAdapter, weapon);
		}
		else
		{
			HideHitRows(weaponView);
		}
		
		
	}	

}
